home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / SPR5 / CONVERT / SPR2ANI.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-27  |  6.1 KB  |  241 lines

  1. uses Crt, AniVGA;
  2.  
  3. type
  4.   sprite_array =  array [0..2000] of pointer;
  5. var
  6.   Gd, Gm: Integer;
  7.  
  8.   sprites: sprite_array;
  9.   blocksize:array [0..2000] of longint;
  10.  
  11. {
  12. WGT Sprite File to AniVGA (by Kai Rohrbacher)
  13. }
  14.  
  15.  function FileExists(FileName: String): Boolean;
  16. { Boolean function that returns True if the file exists;otherwise,
  17.    it returns False. Closes the file if it exists. }
  18. var
  19.   F: File;
  20. begin
  21.   {$I-}
  22.   Assign(F, FileName);
  23.   Reset(F);
  24.   Close(F);
  25.   {$I+}
  26.   FileExists := (IOResult = 0) and (FileName <> '');
  27.  end;  { FileExists }
  28.  
  29. procedure convertsprites(infilename,outfilename:string);
  30. var
  31. filein:Text; { 256 color sprite file }
  32. fileout:File; { converted sprite file }
  33. palette:array [1..768] of char; { 256 * 3 (RGB values) }
  34. size: longint;
  35. maxcolor,maxsprite:integer;
  36. temp:pointer;
  37. a,b,i,j,spritemade:integer;
  38. buf: array [1..13] of char;  { Used for header }
  39. x,y:integer; { Screen location }
  40. col:char;  { Pixel color }
  41. c1,c2:char;
  42. result:integer;
  43. startingsprite:integer;
  44.  
  45. begin
  46.  
  47.     { Open the files }
  48. if FileExists(infilename) then
  49.    begin
  50.     Assign(filein,infilename);
  51.     Reset(filein);
  52.     Assign(fileout,outfilename);
  53.     Rewrite(fileout,1);
  54.  
  55.     Read(filein,c1);
  56.     Read(filein,c2);
  57.     a:=256*ord(c2)+ord(c1);
  58.      { Get the version number, and change the startingsprite accordingly.
  59.      If version <= 3, maxsprite contains the maximum number of sprites
  60.      that can be stored in a file.  If version > 4, maxsprite contains
  61.      the number of the highest sprite in the file. (empty sprites at
  62.      the end are not kept in the file. }
  63.     if (a <= 3) then
  64.       startingsprite := 1
  65.     else startingsprite := 0;    { Version 4 starts at sprite 0 }
  66.  
  67.     for i:=1 to 13 do
  68.     read(filein,buf[i]); { sprite header }
  69.  
  70.     if (buf=' Sprite File ') then { see if it is a sprite file }
  71.        begin
  72.         for i:=1 to 768 do
  73.          read(filein,palette[i]); { Read in 256 color palette }
  74.     maxcolor:=256;
  75.     blockwrite(fileout,maxcolor,2); { Write the number of colors stored in file }
  76.  
  77.         blockwrite(fileout,palette,maxcolor*3);
  78.         { write palette }
  79.  
  80.         Read(filein,c1);
  81.         Read(filein,c2);
  82.         maxsprite:=256*ord(c2)+ord(c1);  { maximum sprites in this file }
  83.     blockwrite(fileout,maxsprite,2);
  84.     for i:= startingsprite to maxsprite do
  85.            begin { load them in }
  86.             Read(filein,c1);
  87.             Read(filein,c2);
  88.             spritemade:=256*ord(c2)+ord(c1); { flag to see if sprite exists }
  89.         blockwrite(fileout,spritemade,2);
  90.         if (spritemade = 1) then
  91.            begin
  92.                 FillPage(1,0);     { maximum sprite size }
  93.                 Read(filein,c1);
  94.                 Read(filein,c2);
  95.                 a:=256*ord(c2)+ord(c1); { width }
  96.                 Read(filein,c1);
  97.                 Read(filein,c2);
  98.                 b:=256*ord(c2)+ord(c1); { height }
  99.         blockwrite(fileout,a,2); { put width and height }
  100.         blockwrite(fileout,b,2);
  101.  
  102.         { Read in the image data. Each byte represents a color
  103.         from 0-255. Obviously converting sprites that use more colors
  104.         than the current mode allows will not work. Draw sprites using
  105.         only the first colors (eg 0-16), up to maxcolors of the mode you're using. }
  106.         for y:=0 to b-1 do
  107.           for x:=0 to a-1 do
  108.                      begin
  109.                       read(filein,col);
  110.               putpixel(x,y,ord(col));
  111.                      end;
  112.  
  113.                 size:=a*b+4;
  114.                 temp:= GetImage(0,0,a-1,b-1,1); { Get the image in new mode }
  115.         blockwrite(fileout,temp^,size); { Write the data in getimage format }
  116.             freemem(temp,size);
  117.            end;
  118.            end;
  119.            Close(filein);
  120.            Close(fileout);
  121.        end;
  122.     end
  123.   else
  124.      begin
  125.        closeroutines;
  126.        writeln('Could not open 256 color sprite file');
  127.        halt(1);
  128.      end;
  129. end;
  130.  
  131. procedure freesprites(freespr:sprite_array);
  132. var
  133. i:integer;
  134. width,height:integer;
  135. begin
  136.  
  137. for i:=0 to 2000 do
  138.   begin
  139.   if freespr[i] <> nil then
  140.     freemem(freespr[i],blocksize[i]);
  141.   end;
  142. end;
  143.  
  144.  
  145.  
  146. procedure loadsprites(infilename:string;var loadspr:sprite_array);
  147. var
  148. filein:file;   { converted color sprite file }
  149. maxcolor,maxsprite:integer;
  150. size:longint;
  151. temp: pointer;
  152. a,b,i,j,spritemade:integer;
  153. red,green,blue:char;
  154. buf:Pchar;
  155. pal:palette;
  156. begin
  157.     { Open the files }
  158.     if FileExists(infilename) then
  159.      begin
  160.       Assign(filein,infilename);
  161.       Reset(filein,1);
  162.       blockread(filein,maxcolor,2);
  163.  
  164.       getpalette(pal);
  165.       for i:=0 to maxcolor-1 do
  166.         begin
  167.           blockread(filein,pal[i].red,1);
  168.           blockread(filein,pal[i].green,1);
  169.           blockread(filein,pal[i].blue,1);
  170.       end;
  171.       SetPalette(pal,TRUE);
  172.  
  173.       blockread(filein,maxsprite,2);   { maximum sprites in this file }
  174.  
  175.     for i := 0 to maxsprite-1 do { load them in }
  176.        begin
  177.         blockread(filein,spritemade,2); { flag to see if sprite exists }
  178.         if (spritemade = 1) then
  179.            begin
  180.         blockread(filein,a,2); { get width and height }
  181.         blockread(filein,b,2);
  182.  
  183.         size := a*b+4; { get byte size of image }
  184.                 blocksize[i]:=size;
  185.                 getmem(loadspr[i],size);
  186.         if (loadspr[i] = nil) then
  187.             begin
  188.               closeroutines;
  189.               writeln('Error: not enough heap space in loadsprites().');
  190.               freesprites(loadspr);
  191.               halt(1);
  192.            end;
  193.                 blockread(filein,loadspr[i]^,size);
  194.            end
  195.          else loadspr[i]:=nil;
  196.  
  197.          end;
  198.     close(filein);
  199.   end;
  200. end;
  201.  
  202.  
  203.  
  204. procedure testsprites;
  205. { Loops through 10 sprites, displaying them on the screen
  206.  using the putimage method. Press a key to go to the next sprite. }
  207. var
  208. i,j:integer;
  209. begin
  210.  
  211. for i:=0 to 10 do
  212.   begin
  213.     FillPage(1,0);
  214.  
  215.    if sprites[i] <> nil then
  216.      begin
  217.      for j:=1 to 20 do
  218.        PutImage(random(320),random(200),sprites[i],1);
  219.        { Put the converted image on the screen }
  220.      while not keypressed do;
  221.      readkey;
  222.      end;
  223.   end;
  224. end;
  225.  
  226. begin
  227.  
  228.   InitGraph;
  229.  
  230.  
  231.    convertsprites('sprtconv.spr','out.spr');
  232.    loadsprites('out.spr',sprites);
  233.    testsprites;
  234.    freesprites(sprites);
  235.  
  236.  
  237.    { clean up }
  238.    Closeroutines;
  239. end.
  240.  
  241.